const http = require('./common/http')
const storage = require('./common/storage')
const hx = require("hbuilderx");
//全局变量
var chapters =[]//当前小说所有章节目录
var index = 0 //当前正在阅读的章节索引
//获取小说章节目录
function getChapters({
	title, 
	fictionId
}) {
	hx.window.setStatusBarMessage(`正在获取${title}所有章节`)
	http(`https://api.pingcc.cn/fictionChapter/search/${fictionId}`).then(({
		code,
		msg,
		data
	}) => {
		if (code == 0) {
			let v = data.chapterList.map((item, index) => {
				return {
					...item,
					label: item.title,
					i: index
				}
			})
			chapters = v
			storage.setData(chapters)
			pickFun(v, 'chapter')
		} else {
			hx.window.showErrorMessage(msg);
		}
		hx.window.clearStatusBarMessage();
	})
}
//获取小说章节内容
function getContent({
	chapterId,
	title,
	i
}) {
	hx.window.setStatusBarMessage(`正在获取${title}内容`)
	http(`https://api.pingcc.cn/fictionContent/search/${chapterId}`).then(({
		code,
		msg,
		data
	}) => {
		if (code == 0) {
			let outputChannel = hx.window.createOutputChannel(title);
			outputChannel.show();
			let content = data.join('\n')
			content = '\n' + content
			outputChannel.appendLine(content);
			index = i
		} else {
			hx.window.showErrorMessage(msg);
		}
		hx.window.clearStatusBarMessage();
	})

}
/**选择小说或者目录的方法
 * @param {Object} data pick需要的数据
 * @param {Object} type fiction选择小说 chapter选择章节
 */
function pickFun(data, type) {
	const pickResult = hx.window.showQuickPick(data, {
		placeHolder: '请选择您心仪的小说'
	});

	pickResult.then(function(result) {
		if (!result) {
			return;
		}
		//获取用户选择的小说目录
		if (type === 'fiction') getChapters(result)
		//获取用户选择的章节内容
		if (type === 'chapter') getContent(result)
	})
}
/**
 * 切换章节
 * @param {Object} type prev上一章节 next下一章节
 */
const toggleChapter=(type)=> {
	if (chapters.length === 0) {
		hx.window.showErrorMessage('当前没有正在阅读的小说');
		return
	}
	if (type === 'prev') {
		if (index === 0) {
			hx.window.showWarningMessage('已经是第一章了');
			return
		}
		index -= 1
		getContent(chapters[index])
	}
	if (type === 'next') {
		if (index === (chapters.length - 1)) {
			hx.window.showWarningMessage('已经是最后一章了');
			return
		}
		index += 1
		getContent(chapters[index])
	}
}

//该方法将在插件激活的时候调用
async function activate(context) {
	chapters = await storage.getData()
	console.log('chapters',chapters)
	//搜索小说
	hx.commands.registerCommand('cajviewer.search', () => {
		let searchPromise = hx.window.showInputBox({
			prompt: '摸鱼神器-书籍搜索',
			placeHolder: '请输入书名',
			value: '百倍修炼'
		})
		searchPromise.then((val) => {
			hx.window.setStatusBarMessage(`正在搜索${val}相关小说...`)
			http(`https://api.pingcc.cn/fiction/search/title/${val}`).then(({
				code,
				msg,
				data
			}) => {
				if (code == 0) {
					let v = data.map(item => {
						return {
							...item,
							label: item.title,
							description: item.descs,
						}
					})
					//前往选择小说
					pickFun(v, 'fiction')
				} else {
					hx.window.showErrorMessage(msg);
				}
				hx.window.clearStatusBarMessage();
			})
		});
	})
	//查看所有目录
	hx.commands.registerCommand('cajviewer.chapter', () => {
		if (chapters.length === 0) {
			hx.window.showErrorMessage('请先选择小说再查看目录');
			return
		}
		pickFun(chapters, 'chapter')
	})
	// 上一章
	hx.commands.registerCommand('cajviewer.prev', () => {
		toggleChapter('prev')
	})
	//下一章
	hx.commands.registerCommand('cajviewer.next', () => {
		toggleChapter('next')
	})
}
//该方法将在插件禁用的时候调用（目前是在插件卸载的时候触发）
function deactivate() {

}
module.exports = {
	activate,
	deactivate
}